﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using FlickrNet;

namespace TagFlo
{
    /// <summary>
    /// Interaction logic for ConfigDialog.xaml
    /// </summary>
    public partial class SettingsDialog : Window
    {
        Configuration config;

        

// Store the Frob in a private variable
private string tempFrob;
Flickr flickr;
private string ApiKey = "c2c7061315df6b1da603bd3ab2d164e4";
private string SharedSecret = "139d4ebe62dee130";

protected void flickrAuthButton_Click(object sender, EventArgs e)
{
    // Create Flickr instance
    // Get Frob    
    tempFrob = flickr.AuthGetFrob();
    // Calculate the URL at Flickr to redirect the user to
    string flickrUrl = flickr.AuthCalcUrl(tempFrob, AuthLevel.Write);
    // The following line will load the URL in the users default browser.
    System.Diagnostics.Process.Start(flickrUrl);
    flickrAuthButton.IsEnabled = false;
    flickrCompleteButton.IsEnabled = true;
    flickrStep2Label.Foreground = Brushes.DarkGray;
    flickrStep1Label.Foreground = Brushes.LightGray;
}

protected void CompleteAuthButton_Click(object sender, EventArgs e)
{
    // Create Flickr instance
    try 
    {
        // use the temporary Frob to get the authentication
        Auth auth = flickr.AuthGetToken(tempFrob);
        // Store this Token for later usage, 
        // or set your Flickr instance to use it.
        Console.WriteLine("User authenticated successfully");
        Console.WriteLine("Authentication token is " + auth.Token); 
        flickr.AuthToken = auth.Token;
        config.FlickerToken = auth.Token;
        config.FlickrUserId = auth.User.UserId;
        config.FlickrUsername = auth.User.Username;
        flickrCompleteButton.IsEnabled = false;
        flickrStep2Label.Foreground = Brushes.LightGray;
        flickrDoneLabel.Foreground = Brushes.DarkGray;
    
Console.WriteLine("User id is " + auth.User.UserId);
    }
    catch(FlickrException ex)
    {
        flickrCompleteButton.IsEnabled = false;
        flickrStep2Label.Foreground = Brushes.LightGray;
        flickrDoneLabel.Content = "Error!";
        flickrDoneLabel.Foreground = Brushes.DarkGray;
    
        // If user did not authenticat your application 
        // then a FlickrException will be thrown.
        Console.WriteLine("User did not authenticate you");
        Console.WriteLine(ex.ToString());
    }
}

        public SettingsDialog(Configuration c)
        {
            config = c;
            InitializeComponent();
            flickr = new Flickr(ApiKey, SharedSecret);
    
        }

       

       
    }
}
